/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SyDeck.sdCities;

//import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityListener;
import org.bukkit.event.entity.ExplosionPrimeEvent;

/**
 *
 * @author Deck
 */
public class Entitydmg extends EntityListener {

    @Override
    public void onExplosionPrime(ExplosionPrimeEvent event) {
        //super.onExplosionPrime(event);
        event.setRadius((float) 2);
        rpgBlockListener Rbl = new rpgBlockListener();
        int x = event.getEntity().getLocation().getBlockX();
        int z = event.getEntity().getLocation().getBlockZ();
        int radius = 3;
        if (Rbl.izcity(x, z) || Rbl.izcity(x + radius, z + radius) || Rbl.izcity(x - radius, z + radius) || Rbl.izcity(x + radius, z - radius) || Rbl.izcity(x - radius, z - radius) || Rbl.izcity(x, z + radius) || Rbl.izcity(x + radius, z) || Rbl.izcity(x - radius, z) || Rbl.izcity(x, z - radius)) {
            event.setCancelled(true);
            event.setRadius(0);
        }
    }

    @Override
    public void onEntityDamage(EntityDamageEvent event) {
        CityInfo ci = new CityInfo();
        String City= ci.CityNameByBlock((int)event.getEntity().getLocation().getX(), (int)event.getEntity().getLocation().getZ());
        if (event instanceof EntityDamageByEntityEvent && City !=null){
            EntityDamageByEntityEvent sub = (EntityDamageByEntityEvent) event;
            if ((sub.getDamager() instanceof Player) && (sub.getEntity() instanceof Player)) {
                Player attacker = (Player) sub.getDamager();
                if (attacker != null) {
                    attacker.sendMessage(ChatColor.RED + "PVP is disabled inside cities.");
                     event.setCancelled(true);
                     return;
                }
            }
        }
    }
}
